package com.mou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mou.dto.RolePageQueryDTO;
import com.mou.entity.Role;
import com.mou.entity.User;
import com.mou.mapper.RoleMapper;
import com.mou.mapper.UserMapper;
import com.mou.result.PageResult;
import com.mou.service.RoleService;
import com.mou.vo.SelectRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult pageQuery(RolePageQueryDTO rolePageQueryDTO) {
        PageHelper.startPage(rolePageQueryDTO.getPage(), rolePageQueryDTO.getPageSize());
        Page<Role> page = roleMapper.pageQuery(rolePageQueryDTO);
        long total = page.getTotal();
        List<Role> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        userMapper.updateByRoleId(id);
        roleMapper.deleteById(id);
    }

    @Override
    public List<SelectRoleVO> getSelectRole() {
        List<SelectRoleVO> selectRoleVOList = roleMapper.getSelectRole();
        return selectRoleVOList;
    }
}
